package au.com.handsunited.dao.hibernate;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.Query;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import au.com.handsunited.dao.ProjectDAO;
import au.com.handsunited.model.Project;
import au.com.handsunited.model.Project;

/**
 *
 * @author arunda
 */
public class ProjectDAOHibernate extends EntityDAOHibernate implements
		ProjectDAO {

	private static Logger logger = LoggerFactory.getLogger(ProjectDAOHibernate.class);


	/**
	 *
	 */
	public ProjectDAOHibernate() {
		super();
	}

	/**
	 * @see au.com.handsunited.dao.impl.ProjectDAO#searchForProjectsByKeywords(java.lang.String)
	 */
	@Override
	public List<Project> searchForProjectsByKeywords(String keywords) {

		FullTextSession fullTextSession = Search.createFullTextSession(getSession());
//		Transaction tx = fullTextSession.beginTransaction();

		MultiFieldQueryParser parser = new MultiFieldQueryParser(
				new String[]{"name", "summary", "description"}, new StandardAnalyzer());
		Query query = null;

		try {
			query = parser.parse(keywords);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery( query, Project.class );
		List<Project> projects = hibQuery.list();

		return projects;

//		return getProjects();
	}


	/**
	 * @see au.com.handsunited.dao.ProjectDAO#searchForProjectsByCriteria(au.com.handsunited.model.Project)
	 */
	@Override
	public List<Project> searchForProjectsByCriteria(Project searchCriteria) {

		Criteria criteria = getSession().createCriteria(Project.class);

		if (StringUtils.isNotBlank(searchCriteria.getName())) {
			criteria.add(
					Restrictions.ilike("name", "%"+searchCriteria.getName()+"%"));
		}

		if (StringUtils.isNotBlank(searchCriteria.getLocation().getSuburb()) ||
				StringUtils.isNotBlank(searchCriteria.getLocation().getCity()) ||
				StringUtils.isNotBlank(searchCriteria.getLocation().getState())) {


			if (StringUtils.isNotBlank(searchCriteria.getLocation().getSuburb())) {
				criteria.add(Restrictions.ilike(
						"location.suburb",
						searchCriteria.getLocation().getSuburb(),
						MatchMode.ANYWHERE)
				);
			}

			if (StringUtils.isNotBlank(searchCriteria.getLocation().getCity())) {
				criteria.add(Restrictions.ilike(
						"location.city",
						searchCriteria.getLocation().getCity(),
						MatchMode.ANYWHERE)
				);
			}

			if (StringUtils.isNotBlank(searchCriteria.getLocation().getState())) {
				criteria.add(Restrictions.ilike(
						"location.state",
						searchCriteria.getLocation().getState(),
						MatchMode.ANYWHERE)
				);
			}
		}

		List<Project> projects = criteria.list();
		return projects;
	}




	/**
	 * @see au.com.handsunited.dao.impl.ProjectDAO#getProject(java.lang.Long)
	 */
	@Override
	public Project getProject(Long projectId) {
		return (Project) getEntity(projectId, Project.class);
	}

	/**
	 * @see au.com.handsunited.dao.impl.ProjectDAO#getProjects()
	 */
	@Override
	public List<Project> getProjects() {
		return getHibernateTemplate().find("from Project");
	}


	/**
	 * @see au.com.handsunited.dao.impl.ProjectDAO#createProject(au.com.handsunited.model.Project)
	 */
	@Override
	public Project createProject(Project project) {
		getHibernateTemplate().saveOrUpdate(project);
		return project;
	}

	/**
	 * @see au.com.handsunited.dao.impl.ProjectDAO#updateProject(au.com.handsunited.model.Project)
	 */
	@Override
	public Project updateProject(Project project) {
		return (Project) updateEntity(project);
	}

}
